package com.yh.correct.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yh.correct.model.Message;
import com.yh.correct.model.ResultData;
import com.yh.correct.service.IMessageService;

/**
 * 信息管理控制层
 * 
 * @author 崔赛赛
 */
@RestController
@RequestMapping("/information")

public class MessageController {
	    /**
		 * 实例化信息服务接口
	     */
	@Autowired
	private IMessageService iMessageService;
	 /**
	 * 添加信息
     */
	@RequestMapping("/add")
	public ResultData add(Message message) {
		
		Message me=new Message();
		me.setContent("你很好1156555556");
		me.setSendTime(new Date());
		me.setStatus(0);
		me.setSendId(2);
		//me.setTelephone("18613717382");
		me.setReceiveId(155);
		
		ResultData rd = new ResultData();
		List<Message> m=new ArrayList<Message>();
		m.add(me);
		int i=iMessageService.addMessage(me);
		if(i>0){
		rd.setCode(0);
		rd.setMessage("插入成功！");
		rd.setData(m);}
		else{
			
			rd.setCode(-1);
			rd.setMessage("插入0！");
			
		}
		return rd;
	}
	 /**
	 * 删除信息
     */
	@RequestMapping("/del")
     public ResultData del(Message message){
    	 Integer id = 4;
		iMessageService.delMessage(id);
    	 ResultData rd = new ResultData();
    	 rd.setCode(0);
 		rd.setMessage("删除成功！");
 		
		return rd;
    	 
    	 
     }
	 /**
	 * 编辑信息
     */
	@RequestMapping("/edit")
	 public ResultData edit(Message message){
		Message me=new Message();
		me.setContent("李凯你老爸让我抓你回家 ");
		me.setSendTime(new Date());
		me.setStatus(0);
		me.setSendId(2);
		//me.setTelephone("18613717382");
		me.setReceiveId(186134856);
		me.setId(5);
		List<Message> ms=new ArrayList<Message>();
		ms.add(me);
		int i = iMessageService.editMessage(me);
   	    ResultData rd = new ResultData();
   	    if(i>0){
   	    rd.setCode(0);
 		rd.setMessage("修改成功！");
 		rd.setData(ms);
   	    }
   	   
   	 else{
			
			rd.setCode(-1);
			rd.setMessage("修改失败！");
			rd.setData(ms);
		}
		return rd;
		
	}
	 /**
	 * 查询所有信息
     */
	@RequestMapping("/getAll")
	 public ResultData getAll(Message message){
  	    
		List<Message> m= iMessageService.getALLMessage();
  	    ResultData rd = new ResultData();
  	    rd.setCode(0);
		rd.setMessage("查看成功！");
		
		rd.setData(m);
		return rd;
		
	}
	 /**
	 * 根据id查询信息
     */
	@RequestMapping("/get")
	 public ResultData get(Message message){
		 ResultData rd = new ResultData();
		 List<Message>mm=new ArrayList<Message>();
		 Message i =	 iMessageService.getMessage(5);
		 mm.add(i);
		 rd.setCode(0);
		 rd.setMessage("查询成功！");
		 rd.setData(mm);
		 return rd;
	 }
	
}
